package com.example.demo.service.impl;

import com.example.demo.entity.Guide;
import com.example.demo.entity.GuideItem;
import com.example.demo.mapper.GuideItemMapper;
import com.example.demo.mapper.GuideMapper;
import com.example.demo.service.GuideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class GuideServiceImpl implements GuideService {


    private final GuideMapper guideMapper;
    private final GuideItemMapper guideItemMapper;

    @Autowired
    public GuideServiceImpl(GuideMapper guideMapper,GuideItemMapper guideItemMapper) {
        this.guideMapper = guideMapper;
        this.guideItemMapper = guideItemMapper;
    }

    @Override
    public Map<String, Object> getGuideDetail(Long guideId) {
        Guide guide = guideMapper.selectById(guideId);
        List<GuideItem> items = guideItemMapper.selectByGuideId(guideId);

        // 按 type 分类
        Map<String, List<String>> grouped = items.stream()
            .collect(Collectors.groupingBy(
                GuideItem::getType,
                LinkedHashMap::new,
                Collectors.mapping(GuideItem::getContent, Collectors.toList())
            ));

        Map<String, Object> result = new HashMap<>();
        result.put("title", guide.getTitle());
        result.put("openingHours", grouped.getOrDefault("openingHours", Collections.emptyList()));
        result.put("freeVisitRules", grouped.getOrDefault("freeVisitRules", Collections.emptyList()));
        return result;
    }
}
